package com.ruoyi.jl_bg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jl_bg.domain.bo.JlBgBsBgBo;
import com.ruoyi.jl_bg.domain.vo.JlBgBsBgVo;
import com.ruoyi.jl_bg.domain.JlBgBsBg;
import com.ruoyi.jl_bg.mapper.JlBgBsBgMapper;
import com.ruoyi.jl_bg.service.IJlBgBsBgService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * wbs清单变更主Service业务层处理
 *
 * @author zyb
 * @date 2024-12-23
 */
@RequiredArgsConstructor
@Service
public class JlBgBsBgServiceImpl implements IJlBgBsBgService {

    private final JlBgBsBgMapper baseMapper;

    /**
     * 查询wbs清单变更主
     */
    @Override
    public JlBgBsBgVo queryById(Long bgId){
        return baseMapper.selectVoById(bgId);
    }

    /**
     * 查询wbs清单变更主列表
     */
    @Override
    public TableDataInfo<JlBgBsBgVo> queryPageList(JlBgBsBgBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JlBgBsBg> lqw = buildQueryWrapper(bo);
        Page<JlBgBsBgVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询wbs清单变更主列表
     */
    @Override
    public List<JlBgBsBgVo> queryList(JlBgBsBgBo bo) {
        LambdaQueryWrapper<JlBgBsBg> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JlBgBsBg> buildQueryWrapper(JlBgBsBgBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JlBgBsBg> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getQsId() != null, JlBgBsBg::getQsId, bo.getQsId());
        lqw.eq(bo.getContractId() != null, JlBgBsBg::getContractId, bo.getContractId());
        lqw.eq(bo.getBdId() != null, JlBgBsBg::getBdId, bo.getBdId());
        lqw.eq(StringUtils.isNotBlank(bo.getBgStatus()), JlBgBsBg::getBgStatus, bo.getBgStatus());
        return lqw;
    }

    /**
     * 新增wbs清单变更主
     */
    @Override
    public Boolean insertByBo(JlBgBsBgBo bo) {
        JlBgBsBg add = BeanUtil.toBean(bo, JlBgBsBg.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setBgId(add.getBgId());
        }
        return flag;
    }

    /**
     * 修改wbs清单变更主
     */
    @Override
    public Boolean updateByBo(JlBgBsBgBo bo) {
        JlBgBsBg update = BeanUtil.toBean(bo, JlBgBsBg.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JlBgBsBg entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除wbs清单变更主
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
